const jwt = require('jsonwebtoken');
const fs = require('fs');
const path = require('path');

module.exports = (ctx, decodedToken, token) => {
  let cert = fs.readFileSync(path.join(__dirname, '../rsa/rsa_public_key.pem'));
  let res = false;
  // console.log(decodedToken);
  try {
    let result = jwt.verify(token,cert,{algorithms: ['RS256']}) || {};
    let {exp, iat} = result, current = Math.floor(Date.now() / 1000);
    if(current <= exp) {
      ctx.tokendata = result.data || {};
      res = false;
    } else {
      res = true;
    }
  } catch (e) {
    console.log(e);
  }
  // console.log(res);
  return res;
}